<template>
  <!-- 授权岗位 -->
  <el-dialog title="选择岗位" :visible.sync="visible" width="80%" top="5vh" append-to-body>
    <SearchWithOperate
      :form-options="formOptions"
      btn-items=""
      @onSearch="handleQuery"
    >
    </SearchWithOperate>
    <el-table ref="table" tooltip-effect="light" :data="postList" border @row-click="clickRow" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" />
      <el-table-column label="岗位编号" prop="postId" show-overflow-tooltip />
      <el-table-column label="岗位名称" prop="postName" show-overflow-tooltip />
      <el-table-column label="岗位编码" prop="postCode" show-overflow-tooltip />
      <el-table-column label="岗位排序" prop="postSort" show-overflow-tooltip />
      <el-table-column label="状态" align="center" width="70" prop="status">
        <template slot-scope="scope">
          <span class="stateShell">
            <div v-if="scope.row.status==='0'" class="common-prompt" style="background-color:#67C23A" />
            <div v-else class="common-prompt" style="background-color:#F56C6C" />
            <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
          </span></template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :pager-count="5"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :loading="submitLoading" @click="handleSelectPost">确 定</el-button>
      <el-button @click="cacel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { insertAuthPostAll, unallocatedPostList } from '@/api/system/role'

export default {
  dicts: ['sys_normal_disable'],
  props: {
    // 角色编号
    roleId: {
      type: [Number, String]
    },
    appId: {
      type: [Number, String]
    }
  },
  data() {
    return {
      formOptions: [
        {
          label: '岗位名称',
          prop: 'postName',
          type: 'input',
          placeholder: '请输入岗位名称'
        }
      ],
      // 遮罩层
      visible: false,
      submitLoading: false,
      // 选中数组值
      userIds: [],
      // 总条数
      total: 0,
      // 未授权用户数据
      postList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleId: 0,
        postName: undefined
      },
      insertParams: {
        appId: 0
      }
    }
  },
  methods: {
    // 显示弹框
    show() {
      this.queryParams.postName = null
      this.submitLoading = false
      this.queryParams.roleId = this.roleId
      this.insertParams.appId = this.appId
      this.getList()
      this.visible = true
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row)
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.postIds = selection.map(item => item.postId)
    },
    // 查询表数据
    getList() {
      unallocatedPostList(this.queryParams).then(res => {
        this.postList = res.rows
        this.total = res.total
      })
    },
    /** 搜索按钮操作 */
    handleQuery(data) {
      this.queryParams = Object.assign(this.queryParams, data)
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 取消按钮*/
    cacel() {
      this.queryParams.nickName = null
      this.queryParams.phonenumber = null
      this.getList()
      this.visible = false
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 选择授权用户操作 */
    handleSelectPost() {
      this.submitLoading = true
      const roleId = this.queryParams.roleId
      const appId = this.insertParams.appId
      if (this.postIds && this.postIds.length) {
        const postIds = this.postIds.join(',')
        insertAuthPostAll({ roleId: roleId, postIds: postIds, appId: appId }).then(res => {
          this.$modal.msgSuccess(res.msg)
          if (res.code === 200) {
            this.visible = false
            this.$emit('ok')
          }
        }).catch(err => {
          this.submitLoading = false
          console.error(err)
        })
      } else {
        this.$modal.msgError('请选择要分配的岗位')
        this.submitLoading = false
        return
      }
    }
  }
}
</script>
